// pages/goods/list/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    taste_id:1,
    cate_id:0,
    mode:0,//0 全部 1按类型 2 按品牌
    tasteList:[],
    cateList:[],
    pagination:null,
    list:[],
    currentpage:1,
    searchText:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getCates();
    this.getGood();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  setmode(e){
    console.log('e', e)
    let mode = e.currentTarget.dataset.mode;
    if(mode != 0) {
      this.setData({
        mode:mode
      })
    }
    this.setData({
      taste_id:0,
      cate_id:0,
    })
    this.getGood();
  },
  getCates(){
    const that = this;
    app.api.get(app.getUrl('goodcate')).then((res)=>{
      console.log(res)
      that.setData({
        cateList:res.cates,
        tasteList:res.tastes
      })
    })
  },
  selectCate(e) {
    // 口味
    let cid = e.currentTarget.dataset.cid;
    console.log('cid', cid)
    if(this.data.mode == 1 || this.data.mode == 0) {
        this.setData({
          taste_id: cid,
          cate_id:0,
        })
    }
    else{ // 分类
      this.setData({
        taste_id: 0,
        cate_id:cid,
      })
    }
    this.getGood();
  },
  doSearch(e){
    this.data.searchText = e.detail.value;
    this.getGood();
  },
  getGood(page=1){
    const that = this;
    const data = {
      taste_id:this.data.taste_id,
      cate_id:this.data.cate_id,
      title:this.data.searchText,
      // page:page
    };
    app.api.get(app.getUrl('goods'), data).then((res)=>{
      console.log(res)
      that.setData({
        list:res.data,
        pagination:res.pagination,
      })
    })
  },
  //到底了
  getMoreGoods(e) {
    if(this.data.pagination.current >= this.data.pagination.last_page) return ;
    const that = this;
    const data = {
      taste_id:this.data.taste_id,
      cate_id:this.data.cate_id,
      title:this.data.searchText,
      page:this.data.pagination.current+1
    };
    app.api.get(app.getUrl('goods'), data).then((res)=>{
      console.log(res)
      let list = that.data.list.concat(res.data);
      that.setData({
        list:list,
        pagination:res.pagination,
      })
    })
    console.log("到底了", e)
  },
  navigateJD(e) {
    let index = e.currentTarget.dataset.index;
    let item = this.data.list[index];
    wx.navigateTo({
      url: '/pages/goods/detail/index?id='+item.id,
    })
    return ;
    if(item) {
       
        wx.navigateToMiniProgram({
          appId: 'wx91d27dbf599dff74',
          path: '/pages/item/detail/detail?sku='+item.jd_id,
          envVersion: 'release',
          success(res) {
              // 打开成功
            console.log(res);
          }
        })
    
    }
  }
})